#coding: utf-8
__author__ = "Chengye zhao (chengyezhao@gmail.com)"

from google.appengine.ext import db
from google.appengine.api import urlfetch
import urllib
from xml.dom import minidom
import logging
import time

class Category(db.Model):
	c1 = db.IntegerProperty()
	c2 = db.IntegerProperty()
	c3 = db.IntegerProperty()
	prob_c1 = db.FloatProperty()
	prob_c2 = db.FloatProperty()
	prob_c3 = db.FloatProperty()

	def process(self, t):
		self.prob_c1 = 0.0
		self.prob_c2 = 0.0
		self.prob_c3 = 0.0
		if t != 0:
			self.prob_c1 = float(self.c1) / t
			self.prob_c2 = float(self.c2) / t 
			self.prob_c3 = float(self.c3) / t 

class Word(db.Model):
	content = db.StringProperty(multiline=True)
	total_appear = db.IntegerProperty()
	appear_c1 = db.IntegerProperty()
	appear_c2 = db.IntegerProperty()
	appear_c3 = db.IntegerProperty()
	prob = db.FloatProperty()
	prob_c1 = db.FloatProperty()
	prob_c2 = db.FloatProperty()
	prob_c3 = db.FloatProperty()

	def process(self, t, c1, c2, c3):
		self.prob = 0.0
		self.prob_c1 = 0.0
		self.prob_c2 = 0.0
		self.prob_c3 = 0.0
		if t != 0:
			self.prob = float(self.total_appear) / t
		if c1 != 0:
			self.prob_c1 = float(self.appear_c1) / c1
		if c2 != 0:
			self.prob_c2 = float(self.appear_c2) / c2
		if c3 != 0:
			self.prob_c3 = float(self.appear_c3) / c3


class Product(db.Model):
	url = db.StringProperty()
	site = db.StringProperty()
	siteName = db.StringProperty()
	siteurl = db.StringProperty()
	title = db.TextProperty()
	city = db.StringProperty()
	imageSmall = db.StringProperty()
	imageMedium = db.StringProperty()
	imageBig = db.StringProperty()
	startTime = db.DateTimeProperty()
	endTime = db.DateTimeProperty()
	price = db.FloatProperty()
	originalPrice = db.FloatProperty()
	rebate = db.FloatProperty()
	sold = db.IntegerProperty()
	category = db.IntegerProperty()
	detail = db.TextProperty()
	lat = db.StringProperty()
	lng = db.StringProperty()
	address = db.TextProperty()
	phone = db.StringProperty()

	def __cmp__(self, other):
		if self.sold == other.sold:
			return 0
		return self.sold > other.sold

	def __hash__(self):
		return self.url.__hash__()

	def printXml(self):
		s = "<product>"
		s += "<url>" + self.url.replace('&', "&amp;") + "</url>"
		s += "<site>" + to_str(self.site) + "</site>"
		s += "<siteName><![CDATA[" + self.siteName + "]]></siteName>"
		s += "<siteurl>" + self.siteurl.replace('&', "&amp;")+ "</siteurl>"
		s += "<title><![CDATA[" + self.title + "]]></title>"
		s += "<city>" + to_str(self.city) + "</city>"
		s += "<imageBig>" + self.imageBig.replace('&', "&amp;") + "</imageBig>"
		s += "<imageMedium>" +  self.imageMedium.replace('&', "&amp;") +  "</imageMedium>"
		s += "<imageSmall>" +  self.imageSmall.replace('&', "&amp;") +  "</imageSmall>"
		s += "<startTime>" + to_str(self.startTime) + "</startTime>"
		s += "<endTime>" + to_str(self.endTime) + "</endTime>"
		s += "<price>" + to_str(self.price) + "</price>"
		s += "<originalPrice>" + to_str(self.originalPrice) + "</originalPrice>"
		s += "<rebate>" + ("%1.1f" % to_float(self.rebate)) + "</rebate>"
		s += "<sold>" + to_str(self.sold) + "</sold>"
		s += "<category>" + to_str(self.category) + "</category>"
		s += "<detail><![CDATA[" + to_str(self.detail) + "]]></detail>"
		s += "<lat>" + to_str(self.lat) + "</lat>"
		s += "<lng>" + to_str(self.lng) + "</lng>"
		s += "<address>" + self.address + "</address>"
		s += "<phone>" + to_str(self.phone) + "</phone>"
		s += "</product>"
		return s

SITE_URL = "http://hangzhoutuangou.appspot.com/"

def getText(nodelist):
	rc = []
	for node in nodelist:
		if node.nodeType == node.TEXT_NODE or node.nodeType == 4 :
			rc.append(node.data)
	return ''.join(rc)

def getTextNodeText(parent, tag):
	if len(parent.getElementsByTagName(tag)) > 0:
		return getText(parent.getElementsByTagName(tag)[0].childNodes)
	return ''

def getTitleTok(title, i):
	#Fetch xml content
	url = "http://173.245.70.96:7778/tok.php?title=" + urllib.quote(title)
	try:
		result = urlfetch.fetch(url=url)
	except urlfetch.Error:
		if i < 3:
			return getTitleTok(title, i+1)
		logging.error('XML %s cannot connect',  url)
		return None
	
	return result

def getProductCategory(title):
	result = getTitleTok(title, 1)
	if not result:
		return None
	#logging.info('Load xml from %s' % url)

	dom = minidom.parseString(result.content)
	#logging.info('dom paresed')

	words = dom.getElementsByTagName("words")[0]
	wordlist = words.getElementsByTagName("word")
	word_tok = []
	for word in wordlist:
		word_tok.append(getText(word.childNodes))

	#get category prob
	q = db.GqlQuery("SELECT * from Category")
	result = q.fetch(1)
	if len(result) == 0:
		return None
	prob_c1 = result[0].prob_c1
	prob_c2 = result[0].prob_c2
	prob_c3 = result[0].prob_c3

	#calc prob
	for tok in word_tok:
		q = db.GqlQuery("SELECT * from Word where content = '" + tok + "'")
		result = q.fetch(1)
		if len(result) > 0:
			#logging.info("Tok %s prob_c1 %f, prob_c2 %f, prob_c3 %f" % (tok, result[0].prob_c1, result[0].prob_c2, result[0].prob_c3))
			prob_c1  = prob_c1 * (result[0].prob_c1 + 0.001) 
			prob_c2  = prob_c2 * (result[0].prob_c2 + 0.001)
			prob_c3  = prob_c3 * (result[0].prob_c3 + 0.001)

	if prob_c1 > prob_c2 and prob_c1 > prob_c3:
		return 1
	elif prob_c2 > prob_c1 and prob_c2 > prob_c3:
		return 2
	else:
		return 3

def to_float(a):
	if a == None or a == "None":
		return 0.0
	try:
		s = float(a)
	except UnicodeEncodeError:
		s = 0.0
	except TypeError:
		s = 0.0
	return s

def to_str(a):
	if a == None or a == "None":
		return ''
	try:
		s = str(a)
	except UnicodeEncodeError:
		s = a
	except TypeError:
		s = a
	return s

def getProductListXml(list):
	s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
	s += "<products>"
	for product in list:
		s += product.printXml()
	s += "</products>"
	return s
